package com.qqpz.sign_project.controller;


import com.google.gson.Gson;
import com.qqpz.sign_project.entity.Location;
import com.qqpz.sign_project.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/v1/location")
public class LocationController {
    @Autowired
    private LocationService locationService;

    @GetMapping("/")
    public String queryAll(){
        List<Location> list = locationService.queryAll();
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("location",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @PostMapping("/near")
    public String queryNear(@RequestBody Map<String,Object> map1){
        Double x=Double.parseDouble(map1.get("x").toString());
        Double y=Double.parseDouble(map1.get("y").toString());
        List<Location> list = locationService.queryByXY(x,y);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("location",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @GetMapping("/name/{name}")
    public String queryByName(@PathVariable("name") String name){
        List<Location> list = locationService.queryByName(name);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("location",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping("/id/{id}")
    public String queryById(@PathVariable("id") String id){
        Location list = locationService.queryById(id);
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("location",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }


    @PostMapping("/")
    public String createLocation(@RequestBody Location location){
        location.setId(UUID.randomUUID().toString());
        System.out.println(location);
        int i= 0;
        try {
            i = locationService.createLocation(location);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @PostMapping("/update")
    public String updateLocation(@RequestBody Location location){
        int i= 0;
        try {
            i = locationService.updateLocation(location);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @PostMapping("/delete")
    public String deleteLocation(@RequestBody Location location){
        int i= 0;
        try {
            i = locationService.deleteLocation(location.getId());
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

}
